// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "HotChunkTool.h"
#include "HotChunkToolStyle.h"
#include "HotChunkToolCommands.h"
#include "Misc/MessageDialog.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "ContentBrowserModule.h"
#include "ContentBrowserDelegates.h"
#include "LevelEditor.h"
#include "AssetRegistryModule.h"
#include "SHotChunkSettings.h"
#include "ILauncherServicesModule.h"
#include "ITargetDeviceServicesModule.h"

static const FName HotChunkToolTabName("HotChunkTool");

#define LOCTEXT_NAMESPACE "FHotChunkToolModule"

void FHotChunkToolModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module

	FHotChunkToolStyle::Initialize();
	FHotChunkToolStyle::ReloadTextures();

	FHotChunkToolCommands::Register();

	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FHotChunkToolCommands::Get().PluginAction,
		FExecuteAction::CreateRaw(this, &FHotChunkToolModule::PluginButtonClicked),
		FCanExecuteAction());

	FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");

	{
		TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender());
		MenuExtender->AddMenuExtension("WindowLayout", EExtensionHook::After, PluginCommands, FMenuExtensionDelegate::CreateRaw(this, &FHotChunkToolModule::AddMenuExtension));

		LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuExtender);
	}

	{
		TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);
		ToolbarExtender->AddToolBarExtension("Settings", EExtensionHook::After, PluginCommands, FToolBarExtensionDelegate::CreateRaw(this, &FHotChunkToolModule::AddToolbarExtension));

		LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
	}

	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(HotChunkToolTabName, FOnSpawnTab::CreateRaw(this, &FHotChunkToolModule::OnSpawnHotChunkTool))
		.SetDisplayName(LOCTEXT("FHotChunkToolTitle", "HotChunkTool"))
		.SetMenuType(ETabSpawnerMenuType::Hidden);

}

void FHotChunkToolModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.
	FHotChunkToolStyle::Shutdown();

	FHotChunkToolCommands::Unregister();
}


void FHotChunkToolModule::AddMenuExtension(FMenuBuilder& Builder)
{
	Builder.AddMenuEntry(FHotChunkToolCommands::Get().PluginAction);
}

void FHotChunkToolModule::AddToolbarExtension(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FHotChunkToolCommands::Get().PluginAction);
}

void FHotChunkToolModule::PluginButtonClicked()
{
	TSharedRef<SDockTab> DockTab = FGlobalTabmanager::Get()->InvokeTab(HotChunkToolTabName);
}

TSharedRef<class SDockTab> FHotChunkToolModule::OnSpawnHotChunkTool(const class FSpawnTabArgs& SpawnTabArgs)
{
	const TSharedRef<SDockTab> DockTab = SNew(SDockTab)
		.Icon(FEditorStyle::GetBrush("Launcher.TabIcon"))
		.TabRole(ETabRole::NomadTab);

	DockTab->SetContent(SNew(SHotChunkSettings));

	return DockTab;
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FHotChunkToolModule, HotChunkTool)